//
//  HMAutioTool.m
//  02-音频播放
//
//  Created by apple on 15-3-22.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "HMAutioTool.h"

@implementation HMAutioTool

static NSMutableDictionary *_soundIDs;

static NSMutableDictionary *_musicPlayers;

+ (void)initialize
{
    _soundIDs = [NSMutableDictionary dictionary];
    
    _musicPlayers = [NSMutableDictionary dictionary];
    
    AVAudioSession *session = [[AVAudioSession alloc] init];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setActive:YES error:nil];
}

#pragma mark - 音乐的播放
/**
 *  根据名称播放音乐
 *
 *  @param musicName 音乐的名称
 */
+ (AVAudioPlayer *)playMusicWithName:(NSString *)musicName
{
    // 1.判断传入的musicName是否为空
    assert(musicName);
    
    // 2.从字典中取播放器
    AVAudioPlayer *player = _musicPlayers[musicName];
    
    // 3.如果取出来的对象为空
    if (player == nil) {
        // 3.1.根据传入的名称,取出对应资源的URL
        NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
        
        // 3.2.判断URL是否为空
        assert(url);
        
        // 3.3.根据URL创建播放器
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        
        // 3.4.将播放器存入字典
        [_musicPlayers setObject:player forKey:musicName];
    }
    
    // 4.准备播放和播放
    [player prepareToPlay];
    [player play];
    
    return player;
}

/**
 *  根据名称暂停音乐
 *
 *  @param musicName 音乐的名称
 */
+ (void)pauseMusicWithName:(NSString *)musicName
{
    // 1.判断传入的musicName是否为空
    assert(musicName);
    
    // 2.从字典中取播放器
    AVAudioPlayer *player = _musicPlayers[musicName];
    
    // 3.如果player有值,且正在播放
    if (player && player.isPlaying) {
        [player pause];
    }
}

/**
 *  根据名称停止音乐
 *
 *  @param musicName 音乐的名称
 */
+ (void)stopMusicWithName:(NSString *)musicName
{
    // 1.判断传入的musicName是否为空
    assert(musicName);
    
    // 2.从字典中取播放器
    AVAudioPlayer *player = _musicPlayers[musicName];
    
    // 3.如果player有值,且正在播放
    if (player) {
        
        // 3.1.停止音乐
        [player stop];
        
        // 3.2.从字典中移除
        [_musicPlayers removeObjectForKey:musicName];
    }
}


#pragma mark - 音效的播放
/**
 *  根据名称播放音频
 *
 *  @param audioName 音频的名称
 */
+ (void)playAudioWithName:(NSString *)audioName
{
    // 1.从字典中取出SoundID
    SystemSoundID soundID = [_soundIDs[audioName] unsignedIntValue];
    
    if (soundID == 0) {
        // 1.获取音频的路径
        NSURL *url = [[NSBundle mainBundle] URLForResource:audioName withExtension:nil];
        
        // 2.加载音频文件
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
        
        // 3.存入字典中
        [_soundIDs setObject:@(soundID) forKey:audioName];
    }
    
    // 4.播放音频(ios8模拟器bug,不能播放,真机没有问题)
    AudioServicesPlayAlertSound(soundID);
}

/**
 *  根据名称销毁音频
 *
 *  @param audioName 音频的名称
 */
+ (void)resposeAudioWithName:(NSString *)audioName
{
    // 1.从字典中取出SoundID
    SystemSoundID soundID = [_soundIDs[audioName] unsignedIntValue];
    
    // 2.如果SoundID有值,则销毁
    if (soundID) {
        AudioServicesDisposeSystemSoundID(soundID);
    }
    
}

@end
